package Servlet;

import Pojo.Judge;
import Pojo.Userlogin;
import Service.Serviceimpl.UserLoginserviceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//处理解决登录问题
public class UserLoginServlet extends HttpServlet {
    private  UserLoginserviceimpl userLoginserviceimpl=new UserLoginserviceimpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       此处需要设置请求头，防止中文乱码问题。
        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html; charset=UTF-8");
        String username=req.getParameter("username");
        String password = req.getParameter("password");
        Userlogin userlogin = new Userlogin(username, password);
        System.out.println(userlogin);
        Judge judgeuserservlet = userLoginserviceimpl.judgeuserservice(userlogin);
        System.out.println(judgeuserservlet);
          if (judgeuserservlet.isResult()){
            if (judgeuserservlet.getState()==1){
                req.getSession().setAttribute("username",username);
                resp.sendRedirect("/web/index.jsp");
            }else {
                req.getSession().setAttribute("username",username);
                resp.sendRedirect("/web/indextwo.jsp");
            }
        }else {
              getServletContext().setAttribute("username",username);
              getServletContext().setAttribute("error","您输入的账号密码错误");
              resp.sendRedirect("/login.jsp");
              System.out.println(getServletContext().getAttribute("error"));
          }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

}
